package com.drug.web.controller.wms;

import com.alibaba.fastjson.JSONObject;
import com.drug.common.annotation.Log;
import com.drug.common.core.controller.BaseController;
import com.drug.common.core.domain.AjaxResult;
import com.drug.common.core.domain.model.LoginUser;
import com.drug.common.core.page.TableDataInfo;
import com.drug.common.enums.BusinessType;
import com.drug.common.utils.SecurityUtils;
import com.drug.common.utils.poi.ExcelUtil;
import com.drug.wms.domain.BusGoodsBaseInfo;
import com.drug.wms.domain.BusGoodsBaseInfoAndSku;
import com.drug.wms.page.BusGoodsBaseInfoAndSkuPage;
import com.drug.wms.page.BusGoodsBaseInfoPage;
import com.drug.wms.service.IBusGoodsBaseInfoService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 商品基本信息Controller
 *
 * @author ruoyi
 * @date 2021-11-09
 */
@RestController
@RequestMapping("/wms/goodsInfo")
public class BusGoodsBaseInfoController extends BaseController {
    @Autowired
    private IBusGoodsBaseInfoService busGoodsBaseInfoService;

    /**
     * 查询商品基本信息列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody JSONObject jsonObject) throws InvocationTargetException, IllegalAccessException {
        BusGoodsBaseInfo busGoodsBaseInfo = new BusGoodsBaseInfo();
        BeanUtils.populate(busGoodsBaseInfo, (Map) jsonObject.get("model"));
        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer pageSize = jsonObject.getInteger("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<BusGoodsBaseInfo> list = busGoodsBaseInfoService.selectBusGoodsBaseInfoList(busGoodsBaseInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品基本信息列表
     */
    @Log(title = "商品基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusGoodsBaseInfo busGoodsBaseInfo) {
        List<BusGoodsBaseInfo> list = busGoodsBaseInfoService.selectBusGoodsBaseInfoList(busGoodsBaseInfo);
        ExcelUtil<BusGoodsBaseInfo> util = new ExcelUtil<BusGoodsBaseInfo>(BusGoodsBaseInfo.class);
        return util.exportExcel(list, "商品基本信息数据");
    }

    /**
     * 获取商品基本信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(busGoodsBaseInfoService.selectBusGoodsBaseInfoById(id));
    }

    /**
     * 新增商品基本信息
     */
    @Log(title = "商品基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusGoodsBaseInfoPage busGoodsBaseInfoPage) {

        return toAjax(busGoodsBaseInfoService.insertBusGoodsBaseInfo(busGoodsBaseInfoPage));
    }

    /**
     * 修改商品基本信息
     */
    @Log(title = "商品基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusGoodsBaseInfoPage busGoodsBaseInfoPage) {
        return toAjax(busGoodsBaseInfoService.updateBusGoodsBaseInfo(busGoodsBaseInfoPage));
    }

    /**
     * 删除商品基本信息
     */
    @Log(title = "商品基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(busGoodsBaseInfoService.deleteBusGoodsBaseInfoByIds(ids));
    }

    /**
     * 获取可销售商品
     *
     * @param busGoodsBaseInfo
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @GetMapping("/getSaleGoods")
    public TableDataInfo getSaleGoods(BusGoodsBaseInfoAndSkuPage busGoodsBaseInfoAndSkuPage) {
        startPage();
        //获取当前部门id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        busGoodsBaseInfoAndSkuPage.setDeptId(loginUser.getDeptId());
        List<BusGoodsBaseInfoAndSku> list = busGoodsBaseInfoService.getSaleGoods(busGoodsBaseInfoAndSkuPage);
        return getDataTable(list);
    }

    /**
     * 获取可销售商品(调货单)
     *
     * @param busGoodsBaseInfo
     * @return
     */
    @GetMapping("/getSaleGoodsFromOther")
    public TableDataInfo getSaleGoodsFromOther(BusGoodsBaseInfoAndSkuPage busGoodsBaseInfoAndSkuPage) {
        startPage();
        List<BusGoodsBaseInfoAndSku> list = busGoodsBaseInfoService.getSaleGoods(busGoodsBaseInfoAndSkuPage);
        return getDataTable(list);
    }
}
